package lzzy;

import java.io.*;
import java.util.Scanner;

public class EncryptAndDecryptFile {
    public static void main(String[] args) throws IOException {
        Scanner sc =new Scanner(System.in);
        System.out.print("选择您要加密的文件：");
        String file_name = sc.nextLine();
        encryption(file_name);
        readFile(file_name);
        jiemi();
    }
    //    加密文件
    public static void encryption(String str) throws IOException{
        BufferedInputStream input = new BufferedInputStream(new FileInputStream(str));
        BufferedOutputStream output = new BufferedOutputStream(
                new FileOutputStream("作业(加密).txt"));
        int n;
        while((n=input.read())!=-1){
            output.write(n+5);
        }
        input.close();
        output.close();
        System.out.println("文件加密成功啦~");
        System.out.println();
    }


    //    读取文件
    public static void readFile(String file_name)throws IOException{
        BufferedReader readNew = new BufferedReader(new FileReader("作业(加密).txt"));
        BufferedReader readOld = new BufferedReader(new FileReader(file_name));
        System.out.println("加密前文件的内容为：");
        while(readOld.ready()){
            System.out.println(readOld.readLine());
        }
        System.out.println();
        System.out.println("加密后文件的内容为：");
        while(readNew.ready()){
            System.out.println(readNew.readLine());
        }
        readOld.close();
        readNew.close();
        System.out.println();
    }

    //    解密文件
    public static void jiemi() throws IOException{
        BufferedInputStream input = new BufferedInputStream(new FileInputStream("作业(加密).txt"));
        BufferedOutputStream output = new BufferedOutputStream(
                new FileOutputStream("作业(解密).txt"));
        int n;

        while((n=input.read())!=-1){
            output.write(n-5);
        }
        input.close();
        output.close();
        System.out.println("文件解密成功啦~");
    }
}
